#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;

class Solution {
public:
    void duplicateZeros(vector<int>& arr)
    {
        int left = 0, right = -1;
        int n = arr.size();
        while (left < n)
        {
            if (arr[left] == 0) right += 2;
            else right++;
            if (right >= n - 1) break;
            left++;
        }

        if (right >= n)
        {
            arr[n - 1] = 0;
            right -= 2;
            left -= 1;
        }

        while (left >= 0)
        {
            if (arr[left] != 0)
            {
                arr[right] = arr[left];
                left--;
                right--;
            }
            else
            {
                arr[right--] = arr[left];
                arr[right] = arr[left];
                left--, right--;
            }
        }
    }
};

int main()
{
    Solution s;
    vector<int> v = { 1,2,3,4,5 };

    s.duplicateZeros(v);
    return 0;
}